/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.flexfield.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;

import com.erp.cux.flexfield.dao.data.DataBox;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldHead;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldHeadCO;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldLine;
import com.erp.cux.flexfield.dao.model.CuxFlexFieldLineCO;
import com.erp.cux.flexfield.service.CuxFlexFieldHeadService;
import com.erp.cux.flexfield.service.CuxFlexFieldLineService;
import com.erp.dataset.dao.model.SysDatasetType;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.dataset.service.SysDatasetService;
import com.erp.dataset.service.SysDatasetTypeService;

@Controller
@RequestMapping("/web/cuxFlexFieldHead")
public class CuxFlexFieldHeadWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(CuxFlexFieldHeadWebController.class);
    
    //服务层注入
    @Autowired
    private CuxFlexFieldHeadService cuxFlexFieldHeadService;
    @Autowired
    private CuxFlexFieldLineService cuxFlexFieldLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private SysDatasetService sysDatasetService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getCuxFlexFieldHeadList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-02-23 18:06:12
     * @author 
     * @param pages
     * @param cuxFlexFieldHeadCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxFlexFieldHeadList")
    public String getCuxFlexFieldHeadList(Pages pages, CuxFlexFieldHeadCO cuxFlexFieldHeadCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<CuxFlexFieldHead> cuxFlexFieldHeadList = this.cuxFlexFieldHeadService.getDataObjects(pages, cuxFlexFieldHeadCO);
        
        //循环获取辅助字段数量
        for(CuxFlexFieldHead cuxFlexFieldHead: cuxFlexFieldHeadList) {
            cuxFlexFieldHead.setFieldNum(this.cuxFlexFieldLineService.getCuxFlexFieldLineCountByHeadCode(cuxFlexFieldHead.getFfHeadCode()));
        }
        
        //字段所属表
        Map flexFieldTableMap = DataBox.getFlexFieldTableMap();
        
        //页面属性设置
        model.addAttribute("cuxFlexFieldHeadList", cuxFlexFieldHeadList);
        model.addAttribute("pages", pages);
        model.addAttribute("flexFieldTableMap", flexFieldTableMap);
        
        return "basic.jsp?content=cuxFlexField/flexFieldList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-02-23 18:06:12
     * @author 
     * @param cuxFlexFieldHead
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxFlexFieldHead")
    public String getCuxFlexFieldHead(CuxFlexFieldHead cuxFlexFieldHead, Model model) {
        //查询要编辑的数据
        if(cuxFlexFieldHead!=null&&StringUtils.isNotBlank(cuxFlexFieldHead.getFfHeadCode())) {
            CuxFlexFieldHead cuxFlexFieldHeadTemp = this.cuxFlexFieldHeadService.getDataObject(cuxFlexFieldHead.getFfHeadCode());
            if(cuxFlexFieldHeadTemp!=null) {
                cuxFlexFieldHead = cuxFlexFieldHeadTemp;
            }
        }
        
        //字段类型
        Map fieldAttributeTypeMap = DataBox.getFieldAttributeTypeMap();
        //字段所属表
        Map flexFieldTableMap = DataBox.getFlexFieldTableMap();
        
        //页面属性设置
        model.addAttribute("cuxFlexFieldHead", cuxFlexFieldHead);
        model.addAttribute("fieldAttributeTypeMap", fieldAttributeTypeMap);
        model.addAttribute("flexFieldTableMap", flexFieldTableMap);
        
        return "basic.jsp?content=cuxFlexField/flexFieldEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-02-23 18:06:12
     * @author 
     * @param cuxFlexFieldHead
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editCuxFlexFieldHead")
    public String editCuxFlexFieldHead(@Valid CuxFlexFieldHead cuxFlexFieldHead, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getCuxFlexFieldHead";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.cuxFlexFieldHeadService.insertOrUpdateDataObject(cuxFlexFieldHead);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("ffHeadCode", cuxFlexFieldHead.getFfHeadCode());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "表名已存在弹性域，请重新输入");
                return "redirect:getCuxFlexFieldHead";
            }else {
                throw e;
            }
        }
        
        return "redirect:getCuxFlexFieldHead";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-02-23 18:06:12
     * @author 
     * @param cuxFlexFieldHead
     * @return String
     *
     */
    @RequestMapping("deleteCuxFlexFieldHead")
    public String deleteCuxFlexFieldHead(CuxFlexFieldHead cuxFlexFieldHead, RedirectAttributes attr) {
        //删除数据前验证数据
        if(cuxFlexFieldHead!=null&&cuxFlexFieldHead.getFfHeadId()!=null&&StringUtils.isNotBlank(cuxFlexFieldHead.getFfHeadCode())) {
            int count = this.cuxFlexFieldLineService.getCuxFlexFieldLineCountByHeadCode(cuxFlexFieldHead.getFfHeadCode());
            
            if(count==0) {
                //删除数据
                this.cuxFlexFieldHeadService.deleteDataObject(cuxFlexFieldHead);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "已配置弹性域字段，无法删除");
            }
        }
        
        return "redirect:getCuxFlexFieldHeadList";
    }
    
    
    
    /**
     * 
     * @description 跳转头自定义辅助字段页面前准备数据
     * @date 2021年2月24日
     * @author dongbin
     * @param model
     * @param attr
     * @return String
     *
     */
    @RequestMapping("beforeHeadFlexField")
    public String beforeHeadFlexField(String ffHeadCode, Model model, RedirectAttributes attr) {
        
        //获取表头的自定义辅助字段
        CuxFlexFieldLineCO cuxFlexFieldLineCO = new CuxFlexFieldLineCO();
        cuxFlexFieldLineCO.setFfHeadCode(ffHeadCode);
        List<CuxFlexFieldLine> cuxFlexFieldLineList = this.cuxFlexFieldLineService.getDataObjects(cuxFlexFieldLineCO);
        
        //循环设置值集
        for(CuxFlexFieldLine cuxFlexFieldLine: cuxFlexFieldLineList) {
            cuxFlexFieldLine.setFieldAttributeDatasetMap(this.sysDatasetService.getDatasetMap(cuxFlexFieldLine.getFieldAttributeDataset()));
        }
        
        //页面属性设置
        model.addAttribute("cuxFlexFieldLineList", cuxFlexFieldLineList);
        
        return "common/flexfield/headFlexField";
    }
}
